package com.api.gbhg.daiyanlu.api;


import com.alibaba.fastjson.JSON;
import com.api.gbhg.daiyanlu.Dao.BudgerProcessDao;
import com.api.gbhg.daiyanlu.DataPrintCopyUtil.PageInfo;
import com.api.gbhg.liuwenbo.utils.AlgorithmUtil;
import com.api.gbhg.liuwenbo.utils.FormatUtil;
import weaver.conn.RecordSet;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 非独立核算预算流程接口
 * Mr.dyl
 */
public class BudgetProcessActionApi {
    private PageInfo[] paIf ;
    private String nd ;
    private Integer dw ;

    public BudgetProcessActionApi() {
    }
    @POST
    @Path("/getProcess")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public String getProcess(Map<String,Object> param){
        if (param.containsKey("dw") && param.get("dw") instanceof Boolean && !(Boolean)param.get("dw")) {
            return JSON.toJSONString((Object)null);
        } else {
            int nd = getNd(param);
            String dw = "2";
            if (param.containsKey("dw") && param.get("dw") instanceof Integer) {
                dw = AlgorithmUtil.getSubCompanIdBybmId((Integer) param.get("dw"));

            }
            PageInfo[] pi = new PageInfo[2];
            Map<String, Integer> num = BudgerProcessDao.getDataCount(nd, 0, dw);
            Map<String, Object>[] map = new HashMap[1];
            pi[0] = new PageInfo();
            map[0] = new HashMap();
            map[0].put("ycl", num.get("processed"));
            map[0].put("dcl", num.get("unprocessed"));
            pi[0].setTitle("核批任务");
            pi[0].setData(map);

            RecordSet rs = BudgerProcessDao.getTotalImplement(nd, 0, dw);
            pi[1] = new PageInfo();
            Map<String, Object>[] maps = new HashMap[]{new HashMap()};
            maps[0] = new HashMap<>();
            if ((rs.getDouble("ze") >= 0)) {
                maps[0].put("yzlje", FormatUtil.DmFormat((rs.getDouble("ze") - rs.getDouble("syed"))));
                maps[0].put("yzlbl", AlgorithmUtil.getRealTimeImplementRate(rs.getDouble("syed"), rs.getDouble("ze")));
            } else {
                maps[0].put("yzlje", FormatUtil.DmFormat(0));
                maps[0].put("yzlbl", AlgorithmUtil.getRealTimeImplementRate(0, 0));
            }
            pi[1].setTitle("预算执行");
            pi[1].setData(maps);
            return JSON.toJSONString(pi);
        }
    }

    public void setNd(Map<String, Object> param) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        if (param.containsKey("nd") && param.get("nd") instanceof Integer) {
            this.nd = param.get("nd").toString();
        } else {
            Date date = new Date();
            this.nd = sdf.format(date.getTime());
        }

    }

    public int getNd(Map<String, Object> param) {
        if(param.containsKey("nd") && param.get("nd") instanceof Integer )
            return  (Integer) param.get("nd");
        else
            return LocalDate.now().getYear();
    }

}
